package com.jinkaimen.goodcash.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.utils.ActivityUtils;
import com.jinkaimen.goodcash.R;
import com.jinkaimen.goodcash.bean.LocationBean;
import com.jinkaimen.goodcash.manager.ApiManager;
import com.jinkaimen.goodcash.ui.adapter.LocationListAdapter;

import java.util.List;

import retrofit2.Call;
import retrofit2.Response;


public class SelectLocationActivity extends BaseActivity {

    public static final int REQUEST_CODE_SELECT_LOCATION_CITY = 100;
    private static final String CITY_ID = "city_id";
    public static final String PARENT_ID = "parent_id";
    public static final String CURRENT_LOCATION = "current_location";
    public static final String BANK_LOCATION = "bank_location";

    private ListView mListView;
    private List<LocationBean> mData;
    private LocationListAdapter mAdapter;
    private String mCurrentLocation;
    private String mParentCode;
    private LocationBean mSelectLocation;

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initIntentData(Intent callIntent) {
        mCurrentLocation = callIntent.getStringExtra(CURRENT_LOCATION);
        mParentCode = callIntent.getStringExtra(PARENT_ID);
    }

    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText("选择地区");
    }

    @Override
    protected int initContentView() {
        return R.layout.activity_select_location;
    }

    @Override
    protected void initChildView() {
        mListView = (ListView) findViewById(R.id.lv_activity_select_location);
        if (mParentCode == null){
            View headerView = View.inflate(this, R.layout.header_activity_select_location, null);

            TextView selectedLocationView = (TextView) headerView.findViewById(R.id.tv_header_activity_select_location_selected_location);
            selectedLocationView.setText(TextUtils.isEmpty(mCurrentLocation)?"请选择":mCurrentLocation);
            mListView.addHeaderView(headerView);
        }
    }

    @Override
    protected void initChildData() {
        if (mParentCode == null){
            mParentCode = "0";
        }

        getCityListFromNet(mParentCode);
    }

    private void getCityListFromNet(String parentCode) {
        ApiManager.getApi().getCityList(
                "INV_HSY001007",
                parentCode
        ).enqueue(new NetCallBack<List<LocationBean>>() {
            @Override
            protected void onSuccess(Call<List<LocationBean>> call, Response<List<LocationBean>> response) {
                mData = response.body();
                mAdapter = new LocationListAdapter(SelectLocationActivity.this, mData);
                mListView.setAdapter(mAdapter);
            }
        });
    }

    @Override
    protected void setChildViewListener() {
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                int index = position;
                if ("0".equals(mParentCode)){
                    index--;
                }

                if (index < 0 || index > mData.size()-1){
                    return;
                }

                LocationBean locationBean = mData.get(index);
                if (!locationBean.isLeaf){

                    mSelectLocation = locationBean;

                    Bundle bundle = new Bundle();
                    bundle.putString(PARENT_ID, locationBean.code);
                    ActivityUtils.startActivityForResult(SelectLocationActivity.this, SelectLocationActivity.class, bundle, REQUEST_CODE_SELECT_LOCATION_CITY);
                } else {
                    Intent returnIntent = new Intent();
                    returnIntent.putExtra("cityCode", locationBean.code);
                    returnIntent.putExtra("areaName", locationBean.aname);
                    setResult(RESULT_OK, returnIntent);
                    ActivityUtils.finishActivity(SelectLocationActivity.this);
                }

            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK){
            switch (requestCode){
                case REQUEST_CODE_SELECT_LOCATION_CITY:
                    String cityName = data.getStringExtra("cityName");

                    if (cityName == null){
                        data.putExtra("cityName", mSelectLocation.aname);
                    }

                    setResult(RESULT_OK, data);
                    ActivityUtils.finishActivity(this);
                    break;
            }
        }
    }
}
